package com.linkoog.devtools.intellij.ui.table.column_info.column;

import com.linkoog.devtools.intellij.ui.table.cell_reander.StringTableCellRenderer;
import com.linkoog.devtools.intellij.ui.table.cell_editor.StringDocumentHandler;
import com.linkoog.devtools.intellij.ui.table.cell_editor.StringDocumentTableCellEditor;
import com.linkoog.devtools.intellij.ui.table.TableRowData;
import com.linkoog.devtools.intellij.ui.table.column_info.provider.TableCellValueGetterProvider;
import com.linkoog.devtools.intellij.ui.table.column_info.provider.TableCellWidthProvider;
import com.linkoog.devtools.intellij.ui.table.column_info.provider.getter.FieldTableCellValueGetterProvider;
import com.linkoog.devtools.intellij.ui.table.column_info.provider.setter.FieldTableCellValueSetterProvider;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class StringColumnInfo<T extends TableRowData> extends AbstractColumnInfo<T, String> {
    private final TableCellWidthProvider tableCellWidthProvider;
    private TableCellValueGetterProvider<T> tableCellValueGetterProvider;
    private FieldTableCellValueSetterProvider<T> tableCellValueSetterProvider;

    private StringDocumentHandler documentHandler;



    public StringColumnInfo(String columnName, String fieldName, TableCellWidthProvider tableCellWidthProvider, StringDocumentHandler documentHandler) {
        super(columnName);
        this.tableCellWidthProvider = tableCellWidthProvider;
        tableCellValueGetterProvider = new FieldTableCellValueGetterProvider<>(fieldName);
        tableCellValueSetterProvider = new FieldTableCellValueSetterProvider<>(fieldName);
        this.documentHandler = documentHandler;
    }

    @Override
    public int getWidth(JTable table) {
        if (tableCellWidthProvider == null) return -1;
        return tableCellWidthProvider.getWidth(table);
    }

    @Nullable
    @Override
    public String valueOf(T rowData) {
        return (String) tableCellValueGetterProvider.valueOf(rowData);
    }

    @Override
    public void setValue(T rowData, String value) {
        tableCellValueSetterProvider.setValue(rowData, value);
    }



    @Override
    public Class<String> getColumnClass() {
        return String.class;
    }

    @Override
    public boolean isCellEditable(final T rowData) {
        return true;
    }

    @Nullable
    @Override
    public TableCellEditor getEditor(final T rowData) {
        return new StringDocumentTableCellEditor(documentHandler);
    }

    @Nullable
    @Override
    public TableCellRenderer getRenderer(T rowData) {
        return new StringTableCellRenderer();
    }




}
